/**
 * Created by archer on 16/4/14.
 */

var InvoiceApp = function(){
	var choose =  1;
	var invoiceId ;
	$( "#date_from" ).datepicker({
    showOtherMonths: true,
    selectOtherMonths: false,
    format: "yyyy-mm-dd"
	});
	$( "#date_to" ).datepicker({
	    showOtherMonths: true,
	    selectOtherMonths: false,
	    format: "yyyy-mm-dd"
	});
    return {
    	start:function(type){
    		if(type == 1){
    			InvoiceApp.shenqing();
    			jQuery("#grid-table1").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
    			setTimeout(function(){
    				$("#grid-table1").setGridWidth($("#shenqing").width());
    			},1);
    		}
    		if(type == 2){
    			InvoiceApp.agreeList();
    			jQuery("#grid-table2").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
    			setTimeout(function(){
    				$("#grid-table2").setGridWidth($("#agree").width());
    			},1);
    		}
    		if(type == 3){
    			InvoiceApp.refuseList();
    			jQuery("#grid-table3").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
    			setTimeout(function(){
    				$("#grid-table3").setGridWidth($("#refuse").width());
    			},1);
    		}
    		choose = type;
    	},
    	startSearch:function(){
    		InvoiceApp.search(choose);
    	},
    	search:function(type){
    		if(type == 1){
    			jQuery("#grid-table1").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
    		}
    		if(type == 2){
    			jQuery("#grid-table2").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
    		}
    		if(type == 3){
    			jQuery("#grid-table3").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
    		}
    	},
    	shenqing:function(){
        	gui.widgets.BootStrap.loadGrid({
                gridSelector:"#grid-table1",
                pagerSelector:"#grid-pager1",
                autowidth:true,
                postData:{
                	companyName:function(){
                		return $.trim($("#companyName").val());
                	},
                	flGmtCreate:function(){
                		return $.trim($("#date_to").val());
                	},
                	ulGmtCreate:function(){
                		return $.trim($("#date_from").val());
                	},
                	state:function(){
                		return '1';
                	},
                },
                jsonReader : {
                    root: "data.rows",
                    records: "data.total",
                    repeatitems : false,
                    total: function (obj) {
                        return  Math.ceil(obj.data.total /jQuery("#grid-table").getGridParam("rowNum"));
                    }
                } ,
                datatype: 'json',
                url:$('#APP_ROOT_PATH').val() + 'invoice/page_data',
                colNames:['申请时间','发票抬头','税号','发票类型','申请人','开票金额(元)','操作'],
                colModel:[
                    {name:'gmtCreate',index:'gmtCreate', width:60,sortable:false},
                    {name:'title',index:'title', width:60,sortable:false},
                    {name:'dutyParagraph',index:'dutyParagraph', width:60,sortable:false},
                    {name:'type',index:'type', width:50,sortable:false,formatter:function(cellvalue,options,rowObject){
                        var aaa = '';
                        if(rowObject.type == '1')
                    		aaa = '增值业务税发票';
                        if(rowObject.type == '2')
                    		aaa = '普通发票';
                    	return aaa;
                    }},
                    {name:'companyName',index:'companyName', width:50,sortable:false},
                    {name:'cash',index:'cash', width:60,sortable:false},
                    {name:'',index:'', width:100,sortable:false,formatter:function(cellvalue,options,rowObject){
                    	var html = '';
                    	html += "<a href='#' onclick='InvoiceApp.agree("+rowObject.id+")'>同意开票</a>|";
                    	html += "<a href='#' onclick='InvoiceApp.openInput("+rowObject.id+")'>拒绝开票</a>";
                    	return html;
                    }},
                ],
                caption: "发票申请",
                rowList:[10,20,30],
                prmNames:{
                    page:"pageNum",
                    rows:"pageSize",
                    sort: "sortField",
                    order: "sortOrder",
                    search:"_search"
                }
            });
        },
        agreeList:function(){
        	gui.widgets.BootStrap.loadGrid({
                gridSelector:"#grid-table2",
                pagerSelector:"#grid-pager2",
                autowidth:true,
                postData:{
                	companyName:function(){
                		return $.trim($("#companyName").val());
                	},
                	flGmtCreate:function(){
                		return $.trim($("#date_to").val());
                	},
                	ulGmtCreate:function(){
                		return $.trim($("#date_from").val());
                	},
                	state:function(){
                		return '2';
                	},
                },
                jsonReader : {
                    root: "data.rows",
                    records: "data.total",
                    repeatitems : false,
                    total: function (obj) {
                        return  Math.ceil(obj.data.total /jQuery("#grid-table").getGridParam("rowNum"));
                    }
                } ,
                datatype: 'json',
                url:$('#APP_ROOT_PATH').val() + 'invoice/page_data',
                colNames:['申请时间','发票抬头','税号','发票类型','申请人','开票金额(元)'],
                colModel:[
                    {name:'gmtCreate',index:'gmtCreate', width:60,sortable:false},
                    {name:'title',index:'title', width:60,sortable:false},
                    {name:'dutyParagraph',index:'dutyParagraph', width:60,sortable:false},
                    {name:'type',index:'type', width:50,sortable:false,formatter:function(cellvalue,options,rowObject){
                        var aaa = '';
                        if(rowObject.type == '1')
                    		aaa = '增值业务税发票';
                        if(rowObject.type == '2')
                    		aaa = '普通发票';
                    	return aaa;
                    }},
                    {name:'companyName',index:'companyName', width:50,sortable:false},
                    {name:'cash',index:'cash', width:60,sortable:false},
                ],
                caption: "开票记录",
                rowList:[10,20,30],
                prmNames:{
                    page:"pageNum",
                    rows:"pageSize",
                    sort: "sortField",
                    order: "sortOrder",
                    search:"_search"
                }
            });
        },
        refuseList:function(){
        	gui.widgets.BootStrap.loadGrid({
                gridSelector:"#grid-table3",
                pagerSelector:"#grid-pager3",
                shrinkToFit:true,
                postData:{
                	companyName:function(){
                		return $.trim($("#companyName").val());
                	},
                	flGmtCreate:function(){
                		return $.trim($("#date_to").val());
                	},
                	ulGmtCreate:function(){
                		return $.trim($("#date_from").val());
                	},
                	state:function(){
                		return '3';
                	},
                },
                jsonReader : {
                    root: "data.rows",
                    records: "data.total",
                    repeatitems : false,
                    total: function (obj) {
                        return  Math.ceil(obj.data.total /jQuery("#grid-table").getGridParam("rowNum"));
                    }
                } ,
                datatype: 'json',
                url:$('#APP_ROOT_PATH').val() + 'invoice/page_data',
                colNames:['申请时间','发票抬头','税号','发票类型','申请人','开票金额(元)','拒绝理由'],
                colModel:[
                    {name:'gmtCreate',index:'gmtCreate', width:60,sortable:false},
                    {name:'title',index:'title', width:60,sortable:false},
                    {name:'dutyParagraph',index:'dutyParagraph', width:60,sortable:false},
                    {name:'type',index:'type', width:50,sortable:false,formatter:function(cellvalue,options,rowObject){
                        var aaa = '';
                        if(rowObject.type == '1')
                    		aaa = '增值业务税发票';
                        if(rowObject.type == '2')
                    		aaa = '普通发票';
                    	return aaa;
                    }},
                    {name:'companyName',index:'companyName', width:50,sortable:false},
                    {name:'cash',index:'cash', width:60,sortable:false},
                    {name:'memo',index:'memo', width:100,sortable:false},
                ],
                caption: "拒绝开票",
                rowList:[10,20,30],
                prmNames:{
                    page:"pageNum",
                    rows:"pageSize",
                    sort: "sortField",
                    order: "sortOrder",
                    search:"_search"
                }
            });
        },
        refuse:function(){
        	var url = $("#APP_ROOT_PATH").val() + 'invoice/refuse';
			gui.util.Ajax.asyncPOST(url,
			{
			"invoiceId": invoiceId,
			"memo" : $.trim($("#memo").val()), 
			},function(res){
				if(res.data.flag){
					$("#myModal").modal("hide");
					jQuery("#grid-table1").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
				}else{
					layer.msg(res.message+" ",{icon :2});
				}
			});
			return false;
        },
        agree:function(id){
        	if(confirm("是否同意")) {
	        	var url = $("#APP_ROOT_PATH").val() + 'invoice/agree';
				gui.util.Ajax.asyncPOST(url,
				{
				"invoiceId": id,
				"memo" : $.trim($("#memo").val()),
				},function(res){
					if(res.data.flag){
						jQuery("#grid-table1").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
					}else{
						layer.msg(res.message+" ",{icon:2});
					}
				});
        	}
			return false;
        },
        openInput : function(id){
        	invoiceId = id;
        	$("#memo").val("");
        	$("#myModal").modal("show");
        },
        addNum : function(id){
        	var url = $("#APP_ROOT_PATH").val() + 'merchants/add_child';
			gui.util.Ajax.asyncPOST(url,
			{
				"shopId": id,
				"addNum" : $("#num").val()
			},function(res){
				if(res.data.flag){
					$("#myModal").modal("hide");
					layer.msg("增加成功",{icon : 1});
					jQuery("#grid-table").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
				}else{
					$.gritter.add({
						title: '错误提示',
						text: '删除失败',
						class_name: 'gritter-error'
					});
				}
			});
        },
    }
}();